#!/usr/bin/python

""" 
    This is the code to accompany the Lesson 2 (SVM) mini-project.

    Use a SVM to identify emails from the Enron corpus by their authors:    
    Sara has label 0
    Chris has label 1
"""
    
import sys
from time import time
sys.path.append("../tools/")
from email_preprocess import preprocess


### features_train and features_test are the features for the training
### and testing datasets, respectively
### labels_train and labels_test are the corresponding item labels
features_train, features_test, labels_train, labels_test = preprocess()




#########################################################
### your code goes here ###
optim_speed = False #True
test_num = len(features_test)

from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB

clf = SVC(kernel='rbf', C=10000.0)
#clf = GaussianNB()

if optim_speed:
  features_train = features_train[:len(features_train)/100]
  labels_train   = labels_train[:len(labels_train)/100]

features_test = features_test[:test_num]
labels_test   = labels_test[:test_num]

t0 = time()
clf.fit(features_train, labels_train)
print("It took %s s for training" % str(round(time()-t0,3)))

t0 = time()
pred = clf.predict(features_test)
print("It took %s s for prediction" % str(round(time()-t0,3)))
pred_chris = [x for x in pred if x == 1]

from sklearn.metrics import accuracy_score
accuracy = accuracy_score(labels_test, pred)

print("Accuracy is", accuracy)
print("pred[10]=%s, pred[26]=%s, pred[50]=%s" % (pred[10],pred[26],pred[50]))
print("label[10]=%s, label[26]=%s, label[50]=%s" % (labels_test[10],labels_test[26],labels_test[50]))
print("There are %s email predicted written by Chris" % len(pred_chris))

#########################################################


